package com.market.service.admin.impl;

import com.github.pagehelper.PageInfo;
import com.market.bean.MarketCategory;
import com.market.bean.MarketCategoryExample;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminCategoryLabelVo;
import com.market.mapper.MarketCategoryMapper;
import com.market.service.admin.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author:WalterChan
 * @Decription:AdminCategoryServiceImpl
 * @Date Created in 2022-12-30-14:54
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {


    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData<AdminCategoryLabelVo> listLable() {
//        查询pid=0 的一级目录数据封装到表中
       List<AdminCategoryLabelVo>list= marketCategoryMapper.selectPidEuqalsZero();

//开启分页
        PageInfo<AdminCategoryLabelVo> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCategory> list() {

//        查询第一次pid=0的，都塞到list中
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);

        List<MarketCategory> List = marketCategoryMapper.selectByExample(example);


//将list中的每一个都查询child
        for (MarketCategory marketCategory : List) {
//            每次循环设置一个新的条件
            MarketCategoryExample example2 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = example2.createCriteria();
//            查询pid=list中元素的id的值，然后赋值给list的孩子
            criteria1.andPidEqualTo(marketCategory.getId());
            criteria.andDeletedEqualTo(false);

          List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example2);
          marketCategory.setChildren(marketCategories);

        }
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(List);
        return CommonData.data(pageInfo);

    }

    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        Date date = new Date();
        marketCategory.setUpdateTime(date);
        marketCategory.setAddTime(date);
//        插入
        int result= marketCategoryMapper.insertSelective(marketCategory);
//        根据generalkey查询插入的值，然后返回

        MarketCategory reslutMarketCategory = marketCategoryMapper.selectByPrimaryKey(marketCategory.getId());
        return reslutMarketCategory;
    }
//    todo 前端数据有问题后续修改
    @Override
    public MarketCategory update(MarketCategory marketCategory) {
        Date date = new Date();
        marketCategory.setUpdateTime(date);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
        MarketCategory result = marketCategoryMapper.selectByPrimaryKey(marketCategory.getId());

        return result;

    }

    @Override
    public void delete(MarketCategory marketCategory) {
        Date date = new Date();
        marketCategory.setDeleted(true);
        marketCategory.setUpdateTime(date);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);


    }


}
